export default {
	data() {
		return {
			// 分享商品弹窗
			shareMask: false,
			// tab 栏
			tabActive: 0,
			isfirst: true,
			showNum: 0,
			hideNum: 0,
			publicNum: 0,

			// 上架
			manageData: {
				total: 0,
				search: '',
				// 已选择的数量
				checkList: [],
				// 筛选数据
				filterList: [],
				// 已筛选数据
				isFilterList: {
					catData: [],
					typeData: '',
					sourceData: '',
					searchData: ''
				}
			}
		}
	},
	components: {},
	methods: {
		// 搜索
		onSearch(val) {
			this.manageData.search = val
			if (!val) {
				this.manageData.isFilterList.searchData = ''
			} else {
				this.manageData.isFilterList.searchData = {
					name: val
				}
			}

			this.resize()
		},
		// 多选
		checkList(item) {
			let data = this.manageData
			if (item.status) {
				data.checkList = data.checkList.filter(filterItem => filterItem != item._optionId || item.id)
				item.status = false
			} else {
				data.checkList.push(item._optionId || item.id)
				item.status = true
			}
		},
		// 切换全选
		changeAllCheck() {
			let data = this.manageData
			let list = this.list.data
			let status = list.length === data.checkList.length
			data.checkList = []
			if (!status) {
				for (let i in list) {
					list[i].status = !status
					data.checkList.push(list[i]._optionId || list[i].id)
				}
			} else {
				for (let i in list) {
					list[i].status = !status
				}
			}
		},
		/** *
         * 上下架通用方法
         * hostFn 为 请求的api函数
         * hostData 为 请求的数据
         * type 为 0 时是下架
         * callFn 为回调函数
         */
		setSaleStatusFn(hostFn, hostData, type, callFn) {
			hostFn(hostData).then(res => {
				if (res.status === 'success') {
					if (type === 0) {
						this.$toast('下架成功!')
					} else {
						this.$toast('上架成功!')
					}
					callFn && callFn()
				}
			})
		},
		/** *
         * 删除通用方法
         * hostFn 为 请求的api函数
         * hostData 为 请求的数据
         * callFn 为回调函数
         */
		delGoodsFn(hostFn, hostData, callFn, promptText) {
			let that = this
			let message = promptText ? `是否要删除该${promptText}？` : '是否确定要删除？'
			uni.showModal({
				title: '确认',
				content: message,
				success: function(res) {
					if (res.confirm) {
						hostFn(hostData).then(res => {
							if (res.status === 'success') {
								that.$toast('删除成功!')
								callFn && callFn()
								that.resize()
							} else {
								that.$toast(res.errors.message)
							}
						})
					}
				}
			})
		},
		/** *
         * 置顶通用方法
         * hostData 为 请求的数据
         * callFn 为回调函数
         */
		setTopBtnFn(hostFn, hostData) {
			hostFn(hostData).then(res => {
				if (res.status === 'success') {
					this.$toast('置顶成功!')
					this.resize()
				} else {
					this.$toast(res.errors.message)
				}
			})
		},
		/** *
         * 数量计算通用方法
         * type 为 0 时是下架, 1 是上架， 2 是删除
         * num 是数量
         */
		calcTabNum(type, num) {
			if (type === 0) {
				this.showNum = this.showNum - num
				this.hideNum = this.hideNum + num
			} else if (type === 1 && this.tabActive === 1) {
				this.showNum = this.showNum + num
				this.hideNum = this.hideNum - num
			} else if (type === 1 && this.tabActive === 2) {
				this.showNum = this.showNum + num
				this.publicNum = this.publicNum - num
			} else {
				this.hideNum = this.hideNum - num
				this.publicNum = this.publicNum + num
			}
		},
		// 切换tab
		changeTab(index, title) {
			this.tabActive = index

			this.clearFilter()

			if (!this.allFilterMaskData) {
				if (index === 2) {
					if (this.filterMaskData[1] && this.filterMaskData[1].hide === false) {
						this.filterMaskData[1].hide = true
					}
					if (this.filterMaskData[2] && this.filterMaskData[2].hide === false) {
						this.filterMaskData[2].hide = true
					}
				} else {
					if (this.filterMaskData[1] && this.filterMaskData[1].hide === true) {
						this.filterMaskData[1].hide = false
					}
					if (this.filterMaskData[2] && this.filterMaskData[2].hide === true) {
						this.filterMaskData[2].hide = false
					}
				}
			}

			// console.log(this.isFirst)
			if (this.isFirst || this.isFirst == false) {
				this.isFirst = true
			}

			this.resize()
		},
		// 清空筛选
		clearFilter() {
			let data = this.manageData

			data.total = 0
			data.search = ''
			// 已选择的数量
			data.checkList = []
			// 筛选数据
			data.filterList = []
			// 已筛选数据
			data.isFilterList = {
				catData: [],
				typeData: '',
				sourceData: '',
				searchData: ''
			}

			if (this.allFilterMaskData) {
				this.filterMaskData = this.tabActive === 2 ? this.commonJS.deepCopy(this.allFilterMaskData) : this.commonJS.deepCopy(this.cloneFilterMaskData)
			} else {
				this.filterMaskData = this.commonJS.deepCopy(this.cloneFilterMaskData)
			}
		}
	}
}
